﻿//Author:Michal Sporna
//License: https://operationsneakpeek.codeplex.com/license

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Util.Classes
{
    //represents an icon next to each interactive obstacle
    public class ObstacleToolIcon
    {
        #region properties
        public Vector2 Position { get; set; }
        public bool IsActive { get; set; } //if avatar is not close enough,the tool button is disabled
        public Vector2 MatherObstaclePos { get; set; } //position of obstacle next to which the icon is draw. Each icon must be drawn next to some obstacle. THis makes an unique ID as no obstacle 
        //can be in the same spot as another one
        public string ObstacleID { get; set; }
        public bool ItemUsed { get; set; }
        #endregion

        #region method

        /// <summary>
        /// constructor
        /// </summary>
        public ObstacleToolIcon()
        {
            this.ItemUsed = false;
        }

        /// <summary>
        /// Draw tool icon
        /// </summary>
        public void Draw()
        {
            try
            {
                if (IsActive)
                {
                    Util.Cache.Draw(Util.Cache.GetTexture("interactionIconEnabled").Text2D, new Rectangle((int)Position.X, (int)Position.Y, 40, 40), Color.White);
                }
                else
                {
                    Util.Cache.Draw(Util.Cache.GetTexture("interactionIconDisabled").Text2D, new Rectangle((int)Position.X, (int)Position.Y, 40, 40), Color.White);
                }


            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return;
            }
        }


        /// <summary>
        /// If tool icon is pressed, use some item
        /// on the obstacle
        /// </summary>
        public bool DoSomethingIfPressed(Vector2 tapPos,Util.Classes.ObstacleToDraw otd)
        {
            //declare
            Obstacle o;
            bool playerHasInteractiveItem=false;//item that interacts with that specific obstacle
            List<Item> itemsInteracting;
            List<double> efficiencies; //list of efficiency values from items that interact with that obstacle
            double theHighestEfficiency;
            Item itemToUse=null;

            try
            {
                //if this button is not active, return false,do nothing
                if (this.ItemUsed)
                {
                    return false;
                }

                //see if this tool item has been pressed...
                if ((tapPos.X >= this.Position.X && tapPos.X <= this.Position.X+40) && (tapPos.Y >= this.Position.Y && tapPos.Y <= this.Position.Y+40))
                {
                    //disable that button
                    IsActive = false;
                    this.ItemUsed = true;

                    //list of items interacting, from those items, the one with highest efficiency will be selected
                    itemsInteracting = new List<Item>();

                    //tool icon pressed
                    //raise event
                    o = Levels.LevelManager.GetObstacleByName(ObstacleID);

                    //check if an item that interacts with that obstacle is in inventory
                    #region is item in the inventory
                 
                    //for (int oi = 0; oi < o.ItemsInteracting.Count; oi++)
                    //{
                    //    for (int ii = 0; ii < Util.Cache.Avatar.CollectedItems.Count; ii++)
                    //    {
                    //        if (o.ItemsInteracting[oi].InteractingItem.ToLower().Trim() == Util.Cache.Avatar.CollectedItems[ii].Name.ToLower().Trim())
                    //        {
                    //            itemsInteracting.Add(Util.Cache.Avatar.CollectedItems[ii]);
                    //        }
                    //    }
                    //}
                    for (int oi = 0; oi < o.ItemsInteracting.Count; oi++)
                    {
                        for (int ii = 0; ii < World.WorldManager.Inventory.Count; ii++)
                        {
                            if (o.ItemsInteracting[oi].InteractingItem.ToLower().Trim() == World.WorldManager.Inventory[ii].ID.ToLower().Trim())
                            {
                                itemsInteracting.Add(World.WorldManager.Inventory[ii].ItemInstance);
                            }
                        }
                    }

                    //select the one item, with the highest efficiency
                    if (itemsInteracting.Count > 0)
                    {
                        playerHasInteractiveItem = true;
                        efficiencies = new List<double>();

                        for (int ii1 = 0; ii1 < itemsInteracting.Count; ii1++)
                        {
                            efficiencies.Add(itemsInteracting[ii1].Efficiency);
                        }
                        //now get the highest one
                        theHighestEfficiency = efficiencies.Max();

                        //now get the item with the highest efficiency, which will be used
                        for (int ii2 = 0; ii2 < itemsInteracting.Count; ii2++)
                        {
                            if (itemsInteracting[ii2].Efficiency == theHighestEfficiency)
                            {
                                itemToUse = itemsInteracting[ii2];
                                break;
                            }
                        }


                    }

                    #endregion

                    if (playerHasInteractiveItem)
                    {
                        for (int i = 0; i < o.Events.Count; i++)
                        {
                            if (o.Events[i].EventType == Helpers.PublicEnums.EventType.OnInteraction)
                            {
                                //assign parameters
                                //o.Events[i].Parameters[0].Parameter = Levels.LevelManager.GetObstacleToDrawByPosition(this.MatherObstaclePos);
                                o.Events[i].Parameters[0].Parameter = otd;
                                o.Events[i].Parameters[1].Parameter = itemToUse;
                                //disable tool icon (so user can not tap on it again)
                                //this.IsActive = false;

                                //raise event
                                World.EventHandler.RaiseEvent(o.Events[i]);
                                break;

                               
                            }
                        }

                        //cleanup
                        itemsInteracting = null;
                        efficiencies = null;
                        theHighestEfficiency = -1;

                        //this tool button has been pressed
                       World.WorldManager.ObstacleInteractionButtonPressed = true;

                        return true;
                    }
                    else
                    {
                        //show information that no required item has been collected...
                     
                        string interactingItems = null;
                        for (int i = 0; i < o.ItemsInteracting.Count; i++)
                        {
                            interactingItems += o.ItemsInteracting[i].InteractingItem.ToLower() + " ";
                        }
                        MessageBox.ShowMessage("You do not have a required Items: "+interactingItems);
                        interactingItems = null;

                        
                        this.ItemUsed = false;

                        //cleanup
                        itemsInteracting = null;
                        efficiencies = null;
                        theHighestEfficiency = -1;

                        return false;
                    }
                    
                }

                this.ItemUsed = false;//item not used yet
                return false;


            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return false;
            }

        }


        #endregion

    }
}
